<template>
	<view class="page-main">
		<liu-slide-img :list="cardList" :type="2" :autoplay="autoplay" :interval="interval" @change="change"
			@click="handleClick"></liu-slide-img>

		<view class="upView" style="background-color: aliceblue;">
			<cc-nineMenuBtn v-for="(btn, index) in menuBtns" :key="index" @menuClick="menuClick(btn.categoryId)"
				:myFlag="btn.myFlag" :myText="btn.myText" :imgSrc="btn.imgSrc"></cc-nineMenuBtn>
		</view>
	</view>
</template>

<script>
	import {
		cardlist,
		Categorylist_T
	} from '../../common/api.js'
	import {
		toLogin,
		clearLoginInfo
	} from '../../utils/auth.js'

	export default {
		data() {
			return {
				cardList: [],
				autoplay: true,
				interval: 3000,
				menuBtns: []
			}
		},
		onLoad() {
			this.loadData()
		},
		methods: {
			change(e) {
				// console.log('==========', e)
			},
			handleClick(index) {
				const url = this.cardList[index].url;
				if (this.isExternalLink(url)) {
					this.openExternalLink(url);
				} else if (this.isValidInternalPath(url)) {
					uni.navigateTo({
						url: url,
					});
				} else {
					console.error('Invalid navigation path:', url);
				}
			},
			isExternalLink(url) {
				return url.startsWith('http://') || url.startsWith('https://');
			},
			isValidInternalPath(path) {
				return path.startsWith('/pages/');
			},
			openExternalLink(url) {
				if (typeof plus !== 'undefined' && plus.runtime) {
					plus.runtime.openURL(url);
				} else {
					window.location.href = url; // For web platforms
				}
			},
			menuClick(categoryId) {
				if (categoryId === 'd1') {
					toLogin();
					return;
				}
				if (categoryId === 'd2') {
					this.logout();
					return;
				}
				uni.navigateTo({
					url: '/pages/answer/answer?categoryId=' + categoryId,
				});
			},
			loadData() {
				cardlist().then((res) => {
					this.cardList = res.data;
					console.log(this.cardList);
				});

				Categorylist_T().then((res) => {
					this.menuBtns = res.data;
					console.log(this.menuBtns);
				});
			}, 
			logout() {
				// 清除登录信息
				clearLoginInfo();
				// 显示退出成功提示
				uni.showToast({
					title: '退出成功',
					icon: 'success',
					duration: 1500
				});
			}
		}
	}
</script>

<style>
	.upView {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		margin-left: 12px;
		width: calc(100vw - 24px);
		margin-top: 10px;
		padding: 4px 0px;
		background-color: white;
	}
</style>



